/*
 *    Thing Game Library
 *   Copyright (C)2005 - 2010 Eduardo Nunes Pereira (eduardonunesp at gmail dot com)
 *
 *   This program/library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 *   This program/library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 *    You should have received a copy of the GNU Lesser General Public
 * License along with this program/library ; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 */

#include <cstdio>
#include <cstdarg>
#include <cassert>
#include <iostream>
#include <sstream>
#include <sys/time.h>
#include <iostream>

#include "Logger.hpp"

Thing::Utils::Logger::~Logger()
{
    finalize();
}

void Thing::Utils::Logger::setLogFile(const std::string& file_name)
{
    /*
    _log_file.open(file_name.c_str(),std::ios_base::trunc);
    if(!_log_file.is_open()) 
    {
        Format fmt("Warning error while open %s for writting");
               fmt % file_name;
        this->error(fmt.str());
    }
    */
}

void Thing::Utils::Logger::log(LogType lt, Format& fmt)
{
    std::string buffer = fmt.str();

    time_t rawtime;
    struct tm * timeinfo;
    char time_buffer [80];

    time ( &rawtime );
    timeinfo = localtime ( &rawtime );

    strftime (time_buffer,80,"%H:%M:%S",timeinfo);

    if(lt & _active_types)
    {
        if (lt & M_DBG)
            std::cout << time_buffer << " - DEBUG " << buffer << "\n";
    }

    /*
    if(_log_file.is_open()) {
        _log_file << time_str.str() << buffer << "\n"; 
    } else {
        error("Logger not open");
    }
    */
}

void Thing::Utils::Logger::log(LogType lt, const std::string& err)
{
    log(lt, Format("%s") % err);
}


void Thing::Utils::Logger::error(const std::string& err)
{
    std::cerr << "Error: " << err << std::endl;
}
